include $(DIR_PREFIX)/Makefile.inc

.PHONY = all
all: lib

.PHONY = lib
lib: libMAIN libLUA_API

CORE_FILES = $(shell ls -1 Core/ | grep -E "*\.cpp")
CORE_SRCS = $(filter %.cpp, $(CORE_FILES))
CORE_OBJS = $(foreach obj_file, $(CORE_SRCS:.cpp=.po), Core/$(obj_file))

EXT_FILES = $(shell ls -1 Ext/ | grep -E "*\.cpp")
EXT_SRCS = $(filter %.cpp, $(EXT_FILES))
EXT_OBJS = $(foreach obj_file, $(EXT_SRCS:.cpp=.po), Ext/$(obj_file))

SDL_FILES = $(shell ls -1 SDL/ | grep -E "*\.cpp")
SDL_SRCS = $(filter %.cpp, $(SDL_FILES))
SDL_OBJS = $(foreach obj_file, $(SDL_SRCS:.cpp=.po), SDL/$(obj_file))

LUA_FILES = $(shell ls -1 Lua/ | grep -E "*\.cpp")
LUA_SRCS = $(filter %.cpp, $(LUA_FILES))
LUA_OBJS = $(foreach obj_file, $(LUA_SRCS:.cpp=.po), Lua/$(obj_file))
	
.PHONY = libMAIN
libMAIN: libCORE libEXT libSDL
	@echo " AR" lib/lib$(LIB_TARGET).a
	$(AR) rcsu $(DIR_PREFIX)/lib/lib$(LIB_TARGET).a $(CORE_OBJS) $(SDL_OBJS) $(EXT_OBJS)
	@echo " LD" bin/lib$(LIB_TARGET).$(LIB_EXT)
	$(CPP) -shared -L$(SDL_LIBS) -o $(DIR_PREFIX)/bin/lib$(LIB_TARGET).$(LIB_EXT) $(CORE_OBJS) $(SDL_OBJS) $(EXT_OBJS) -lSDL -lSDL_image -l$(LUA_LIB) $(IMP_LIB_CORE)

.PHONY = libLUA_API
libLUA_API: libLUA libMAIN
	@echo " LD" bin/lib$(LIB_TARGET)_lua.$(LIB_EXT)
	$(CPP) -shared -L$(DIR_PREFIX)/bin -L$(LUA_LIBS) -o $(DIR_PREFIX)/bin/lib$(LIB_TARGET)_lua.$(LIB_EXT) $(LUA_OBJS) -l$(LIB_TARGET) -l$(LUA_LIB) $(IMP_LIB_LUA)

.PHONY = clean
clean:
	$(MAKE) -C Core/ BUILD=$(BUILD) DIR_PREFIX=$(DIR_PREFIX)/.. clean
	$(MAKE) -C Ext/ BUILD=$(BUILD) DIR_PREFIX=$(DIR_PREFIX)/.. clean
	$(MAKE) -C SDL/ BUILD=$(BUILD) DIR_PREFIX=$(DIR_PREFIX)/.. clean
	$(MAKE) -C Lua/ BUILD=$(BUILD) DIR_PREFIX=$(DIR_PREFIX)/.. clean

.PHONY = libCORE
libCORE: 
	$(MAKE) -C Core/ BUILD=$(BUILD) DEBUG=$(DEBUG) DIR_PREFIX=$(DIR_PREFIX)/..

.PHONY = libEXT
libEXT: 
	$(MAKE) -C Ext/ BUILD=$(BUILD) DEBUG=$(DEBUG) DIR_PREFIX=$(DIR_PREFIX)/..

.PHONY = libSDL
libSDL: 
	$(MAKE) -C SDL/ BUILD=$(BUILD) DEBUG=$(DEBUG) DIR_PREFIX=$(DIR_PREFIX)/..

.PHONY = libLUA
libLUA: 
	$(MAKE) -C Lua/ BUILD=$(BUILD) DEBUG=$(DEBUG) DIR_PREFIX=$(DIR_PREFIX)/..
